package control
{
	import com.greensock.TweenLite;
	
	import events.MyEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	public class DragMoveV extends EventDispatcher
	{
		protected const dragDistance:Number=10;//触摸结束时如果大于这个距离  判定为要移动过
		protected var parent:DisplayObjectContainer;
		protected var startY:Number;
		private var closed:Boolean=false;
		protected var startMouseY:Number;
		public var moveDisatance:Number;
		private var minPosition:Number;
		private var maxPosition:Number;
		public function DragMoveV(target:DisplayObjectContainer,top:Number,bottom:Number)
		{
			parent=target;
			minPosition=top;
			maxPosition=bottom;
			parent.stage.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
		}
		
		public function enable():void
		{
			parent.stage.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
		}
		
		public function disable():void
		{
			parent.stage.removeEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
		}
		
		public function get isClick():Boolean
		{
			return Math.abs(moveDisatance)<dragDistance;
		}
		
		protected function handleMouseDown(e:MouseEvent):void
		{
			parent.dispatchEvent(new MyEvent("moved",null,true));
			if(closed)return;
			startMouseY=parent.root.mouseY;
			startY=parent.y;
			parent.stage.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			parent.root.addEventListener(Event.ENTER_FRAME,listenMousePosition);
		}
		
		
		private function listenMousePosition(e:Event):void
		{
			parent.dispatchEvent(new MyEvent("moved",null,true));
			var nowMouseY:Number=parent.root.mouseY;
			moveDisatance=nowMouseY-startMouseY;
			var targetY:Number=startY+moveDisatance;
			moveTarget(targetY);
		}
		
		
		private function moveTarget(targetY:Number):void
		{
			if(targetY<minPosition)
			{
				targetY=minPosition;
			}
			else if(targetY>maxPosition)
			{
				targetY=maxPosition;
			}
			TweenLite.to(parent,0.5,{y:targetY});
		}
		
		protected function back():void
		{
			var targetY:Number=parent.y;
			if(parent.y<minPosition)
			{
				targetY=minPosition;
			}
			else if(parent.y>maxPosition)
			{
				targetY=maxPosition;
			}
			TweenLite.to(parent,0.5,{y:targetY});
		}
		
		public function set move(distance:Number):void
		{
			startY=parent.root.mouseY;
			moveDisatance=distance;
			var targetY:Number=startY+moveDisatance;
			moveTarget(targetY);
		}
		
		
		protected function handleMouseUp(e:MouseEvent):void
		{
			if(closed)return;
			if(parent.stage)
			{
				parent.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
				parent.root.removeEventListener(Event.ENTER_FRAME,listenMousePosition);
			}
			back();
		}
		
		public function gc():void
		{
			closed=true;
			if(parent.stage.hasEventListener(MouseEvent.MOUSE_DOWN))
			{
				parent.stage.removeEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
				
			}
			if(parent.stage.hasEventListener(MouseEvent.MOUSE_UP))
			{
				parent.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
				
			}
			if(parent.root.hasEventListener(Event.ENTER_FRAME))
			{
				parent.root.removeEventListener(Event.ENTER_FRAME,listenMousePosition);
			}
			
		}
		
	}
}